﻿using System;
using System.Linq;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Modules.ACTP.Components;
using System.Web;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class AddEditTask : AddEditItemControl
    {
        private int _TeamID;
        public int TeamID
        {
            get
            {
                if (_TeamID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TeamID"] != null)
                        _TeamID = Int32.Parse(Request.Params["TeamID"]);
                }
                return _TeamID;
            }
            set { _TeamID = value; }
        }

        private int _ProjectID;
        public int ProjectID
        {
            get
            {
                if (_ProjectID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ProjectID"] != null)
                        _ProjectID = Int32.Parse(Request.Params["ProjectID"]);
                }
                return _ProjectID;
            }
            set { _ProjectID = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditTask_Update;
            this.Delete += AddEditTask_Delete;

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Team.
                Team team = Team.Load(this.TeamID);
                txtTeamName.Text = team.Title;

                // Project.
                Project project = Project.Load(this.ProjectID);
                txtProjectName.Text = project.Subject;

                // TaskMilestones of this project.
                ddlTaskMilestone.DataSource = TaskMilestone.SelectBy_ProjectID(this.ProjectID);
                ddlTaskMilestone.DataBind();

                // Members of this project.
                ddlEmployee.DataSource = Employee.SelectCollectionBy_ProjectID(this.ModuleID, this.ProjectID).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
                ddlEmployee.DataBind();

                // TaskType list.
                ddlTaskType.DataSource = TaskType.SelectCollectionAll(this.ModuleID);
                ddlTaskType.DataBind();

                if (this.ItemID > 0)
                {
                    Task task = Task.Load(this.ItemID);
                    if (task != null)
                    {
                        // ProjectTask.
                        // Not implement

                        txtSubject.Text = task.Subject;
                        ddlTaskType.SelectedValue = task.TaskTypeID.ToString();
                        if (task.StartDate.Year > 1753)
                        {
                            txtStartDate.Text = task.StartDate.ToShortDateString();
                        }

                        if (task.DueDate.Year > 1753)
                        {
                            txtDueDate.Text = task.DueDate.ToShortDateString();
                        }

                        if (task.CompletedDate.Year > 1753)
                        {
                            txtCompletedDate.Text = task.CompletedDate.ToShortDateString();
                        }

                        ddlPriority.SelectedValue = task.Priority;
                        ddlStatus.SelectedValue = task.Status;
                        txtCompletedPercent.Text = task.CompletedPercent.ToString();
                        txtDescription.Text = task.Description;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        void AddEditTask_Update(object sender, EventArgs e)
        {
            Task task = new Task();
            if (this.ItemID > 0)
            {
                task = Task.Load(this.ItemID);
            }

            task.ModuleID = this.ModuleID;
            task.Subject = txtSubject.Text;
            task.TaskTypeID = Convert.ToInt32(ddlTaskType.SelectedValue);
            task.StartDate = txtStartDate.Text.Length > 0 ? Convert.ToDateTime(txtStartDate.Text) : new DateTime(1753, 1, 1);
            task.DueDate = txtDueDate.Text.Length > 0 ? Convert.ToDateTime(txtDueDate.Text) : new DateTime(1753, 1, 1);
            task.CompletedDate = txtCompletedDate.Text.Length > 0 ? Convert.ToDateTime(txtCompletedDate.Text) : new DateTime(1753, 1, 1);
            task.Priority = ddlPriority.SelectedValue;
            task.Status = ddlStatus.SelectedValue;
            task.CompletedPercent = Convert.ToByte(txtCompletedPercent.Text);
            task.Description = txtDescription.Text;

            // Insert.
            if (this.ItemID == 0)
            {
                // Insert Task.
                task.CreatedBy = task.UpdatedBy = PortalSecurity.CurrentUser.Identity.Email;
                task.CreatedDate = task.UpdatedDate = DateTime.Now;
                this.ItemID = task.Insert();

                // Insert ProjectTask
                ProjectTask projectTask = new ProjectTask();
                projectTask.ModuleID = this.ModuleID;
                projectTask.TeamID = this.TeamID;
                projectTask.ProjectID = this.ProjectID;
                projectTask.TaskMilestoneID = Convert.ToInt32(ddlTaskMilestone.SelectedValue);
                projectTask.TaskID = this.ItemID;
                projectTask.EmployeeID = Convert.ToInt32(ddlEmployee.SelectedValue);
                projectTask.Insert();
            }
            // Update.
            else
            {
                // Update Task.
                task.TaskID = this.ItemID;
                task.UpdatedBy = PortalSecurity.CurrentUser.Identity.Email;
                task.UpdatedDate = DateTime.Now;
                task.Update();

                // Delete -> Update ProjectTask.
                ProjectTask.DeleteBy_TaskID(this.ItemID);
                ProjectTask projectTask = new ProjectTask();
                projectTask.ModuleID = this.ModuleID;
                projectTask.TeamID = this.TeamID;
                projectTask.ProjectID = this.ProjectID;
                projectTask.TaskMilestoneID = Convert.ToInt32(ddlTaskMilestone.SelectedValue);
                projectTask.TaskID = this.ItemID;
                projectTask.EmployeeID = Convert.ToInt32(ddlEmployee.SelectedValue);
                projectTask.Insert();
            }

            // Refresh.
            //string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, task.ID, "AddEditTask");
            //Response.Redirect(url);

            // Go back.
            this.ReturnToReferrerPage();
        }

        void AddEditTask_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Task.DeleteTask(this.ItemID);
                // Go back.
                this.ReturnToReferrerPage();
            }
        }
    }
}
